<template>
    <div class="container">
      <!-- <div class="item">
        <span>公司:</span><span>{{ dataFromSupplierInput.company }}</span>
      </div>
      <div class="item">
        <span>公司简称:</span
        ><span>{{ dataFromSupplierInput.companySimple }}</span>
      </div>
      <div class="item">
        <span>经营时间:</span
        ><span>
          {{ dataFromSupplierInput?.stockDataStart }}-{{
            dataFromSupplierInput?.stockData
          }}
        </span>
      </div> -->
      <BaseInfo :dataFromSupplierInput="dataFromSupplierInput"></BaseInfo>
    </div>
  </template>
  
  <script>
  import {res, dataFromSupplierInput, diffAttrList} from "./data.js";
  import BaseInfo from './BaseInfo.vue'
  
  export default {
    components: {
      BaseInfo
  
    },
    name: "base-info",
    data() {
      return {
        res,
        // 拿supplierInfoFromDB的数据 供应商库的供应商信息
        supplierInfoFromDB: {},
        //dataFromSupplierInput  供应商详情信息
        dataFromSupplierInput,
        // diffAttrList,
        diffData: [],
      };
    },
    methods: {
      getSupplierInfoFormDB() {
        const supplierInfoFromDB = this.composeDataFormDB(this.res);
        Object.assign(this.supplierInfoFromDB, supplierInfoFromDB);
      },
      composeDataFormDB(rawData) {
        const cData = {};
        diffAttrList.forEach((item) => {
          // 判断是否是字段数组
          if (Array.isArray(item.attrNameOfDB)) {
            // 再次循环，单独存储
            // 由于对比的字段肯定一致，因此只判断其中一个是否是字段数组就可以，也因此只要针对一个循环存储
            item.attrNameOfDB.forEach((attr) => {
              cData[attr] = rawData[attr] || "";
            });
          } else {
            cData[item.attrNameOfSupplier] = rawData[item.attrNameOfDB];
          }
        });
        return cData;
      },
      getFormDate(timestamp) {
        if (timestamp==='') {
          return ''
        }
        let date = new Date(parseInt(timestamp) * 1000);
        let Year = date.getFullYear();
        let Moth = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
        let Day = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
        let  GMT =  Year + '-' + Moth + '-' + Day;
        return  GMT;
      },
      calculateDiffData() {
        const dataFromSupplierInput = this.dataFromSupplierInput;
        const {supplierInfoFromDB} = this;
        const diffData = [];
        for (const {label, attrNameOfDB, attrNameOfSupplier} of diffAttrList) {
          let dbValue; // 最后用来存储到diffData
          let supplierValue;
          let dada = attrNameOfDB;
          if (typeof attrNameOfDB !== "string") {
            dada = attrNameOfDB[1]
          }
          if (supplierInfoFromDB[dada] === null) {
            dataFromSupplierInput[dada] =dataFromSupplierInput[dada]
            continue
          }
          if (dataFromSupplierInput[dada] === null) {
            dataFromSupplierInput[dada] =dataFromSupplierInput[dada]
            continue
          }
          if (Array.isArray(attrNameOfDB) && Array.isArray(attrNameOfSupplier)) {
            // 把需要对比的两个数组，他们的value存在数组里面
            const propertiesFromDB = attrNameOfDB.map(
                (attr) => this.getFormDate(supplierInfoFromDB[attr]) || ""
            );
            const propertiesFromSupplier = attrNameOfSupplier.map(
                (attr) => dataFromSupplierInput[attr] || ""
            );
            if (propertiesFromDB[0]==="") {
              continue
            }
            // 字段数组
            // 合并成字符串，这一步看你自己需要对数据进行什么操作改
            dbValue = propertiesFromDB.join("-");
            supplierValue = propertiesFromSupplier.join("-");
          } else {
            // 单个字段
            // 直接存
            dbValue = supplierInfoFromDB[attrNameOfDB];
            supplierValue = dataFromSupplierInput[attrNameOfSupplier];
          }
          // 比对，存储
          if (dbValue !== supplierValue) {
            dataFromSupplierInput[dada] = dataFromSupplierInput[dada] + '【X】'
            diffData.push({
              diffPropertyName: label,
              propertyValueFormDB: dbValue,
              propertyValueFormSupplier: supplierValue,
            });
          }else {
            dataFromSupplierInput[dada] = dataFromSupplierInput[dada] +'【√】'
          }
        }
        this.diffData = diffData;
      },
    },
    async mounted() {
      await this.getSupplierInfoFormDB();
      await this.calculateDiffData();
    },
  };
  </script>
  <style scoped></style>
  